﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Specialized;
using System.Collections.Observable;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace ObservableUnitTests
{
    [TestClass]
    public class ObservableDictionaryTest
    {

        ObservableDictionary<int, int> ObservableDictionary;
        List<NotifyCollectionChangedEventArgs> ObservableEvents;

        [TestInitialize]
        public void TestInitialize()
        {
            ObservableDictionary = new ObservableDictionary<int, int>();
            ObservableDictionary[1] = 1;
            ObservableDictionary[2] = 2;
            ObservableDictionary[3] = 3;
            ObservableDictionary[4] = 4;
            ObservableDictionary[5] = 5;

            ObservableEvents = new List<NotifyCollectionChangedEventArgs>();

            ObservableDictionary.CollectionChanged += (s, e) => ObservableEvents.Add(e);
        }

        [TestMethod]
        public void BasicSerialization()
        {
            ObservableDictionary<int,int> deserialized;

            BinaryFormatter formatter = new BinaryFormatter();

            using (TempFile tmpFile = new TempFile())
            {
                FileStream stream = tmpFile.Stream;
                formatter.Serialize(stream, ObservableDictionary);
                stream.Seek(0, SeekOrigin.Begin);
                deserialized = (ObservableDictionary<int,int>)formatter.Deserialize(stream);
            }

            Assert.AreEqual(ObservableDictionary.Count, deserialized.Count);
            Assert.IsTrue(Enumerable.SequenceEqual(ObservableDictionary, deserialized));
        }
    }
}
